package com.fanxj.controller;
import com.fanxj.Utils.ImportResult;
import com.fanxj.service.RelicImportService;
import com.fanxj.service.RelicImportService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.aliyun.oss.model.ObjectMetadata;
import com.fanxj.Utils.OSSService;
import com.fanxj.Utils.ResponseData;
import com.fanxj.domain.*;
import com.fanxj.service.AdminService;
import com.fanxj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/Admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private OSSService ossService;
    @Autowired
    private RelicImportService relicImportService;

    @RequestMapping(value = "/findAllRelic",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findAllRelic(){
        List<Relic> relicList=adminService.findAllRelic();
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("relicList", relicList);
        return responseData;
    }

    @RequestMapping(value = "/findAllUser",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findAllUser(String role){
        List<User> userList=adminService.findAllUser(role);
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("userList", userList);
        return responseData;
    }

    @RequestMapping(value = "/findUserByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findUserByName(String userName){
        List<User> userList=adminService.findUserByName("%"+userName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("userList", userList);
        return responseData;
    }

    @RequestMapping(value = "/findRelicByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findRelicByName(String relicName){
        List<Relic> relicList=adminService.findRelicByName("%"+relicName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("relicList", relicList);
        return responseData;
    }

    @RequestMapping(value = "/deleteUser",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData deleteUser(User user){
        adminService.deleteUser(user);
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    @RequestMapping(value = "/updateUser",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData updateUser(User user){
        //判断数据库中是否已经注册过
        User userExist=userService.findByUser(user);
        ResponseData responseData;
        if(userExist!=null){
            responseData = ResponseData.UserExist();
        }
        else{
            adminService.updateUser(user);
            responseData = ResponseData.ok();
        }
        return responseData;
    }

    @RequestMapping(value = "/deleteRelic",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData deleteRelic(Relic relic){
        adminService.deleteRelic(relic);
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    @RequestMapping(value = "/deleteGroupType",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData deleteGroupType(GroupType groupType){
        adminService.deleteGroupType(groupType);
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    @RequestMapping(value = "/findGroupByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findGroupByName(String searchName){
        List<GroupType> groupList=adminService.findGroupByName("%"+searchName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("groupList", groupList);
        return responseData;
    }

    @RequestMapping(value = "/deleteDecadeType",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData deleteDecadeType(DecadeType decadeType){
        adminService.deleteDecadeType(decadeType);
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    @RequestMapping(value = "/findDecadeByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findDecadeByName(String searchName){
        List<DecadeType> decadeList=adminService.findDecadeByName("%"+searchName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("decadeList", decadeList);
        return responseData;
    }



    @RequestMapping(value = "/deleteLocalType",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData deleteLocalType(LocalType localType){
        adminService.deleteLocalType(localType);
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    @RequestMapping(value = "/findLocalByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findLocalByName(String searchName){
        List<LocalType> localList=adminService.findLocalByName("%"+searchName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("localList", localList);
        return responseData;
    }

    @RequestMapping(value = "/addGroupType",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData addGroupType(GroupType groupType){
        GroupType isexitGroup=adminService.findGroupTypeByCode(groupType);
        ResponseData responseData=null;
        if(isexitGroup==null) {
            adminService.addGroupType(groupType);
            responseData = ResponseData.ok();
        }
        else {
            responseData = ResponseData.UserExist();
        }
        return responseData;
    }

    @RequestMapping(value = "/addDecadeType",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData addDecadeType(DecadeType decadeType){
        DecadeType isexitDecade=adminService.findDecadeTypeByCode(decadeType);
        ResponseData responseData=null;
        if(isexitDecade==null) {
            adminService.addDecadeType(decadeType);
            responseData = ResponseData.ok();
        }
        else {
            responseData = ResponseData.UserExist();
        }
        return responseData;
    }

    @RequestMapping(value = "/addLocalType",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData addLocalType(LocalType localType){
        LocalType isexitLocal=adminService.findLocalTypeByCode(localType);
        ResponseData responseData=null;
        if(isexitLocal==null) {
            adminService.addLocalType(localType);
            responseData = ResponseData.ok();
        }
        else {
            responseData = ResponseData.UserExist();
        }
        return responseData;
    }

    @RequestMapping(value = "/findAllCollection",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findAllCollection(){
        List<AdminCollection> collectionList=adminService.findAllCollection();
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("collectionList", collectionList);
        return responseData;
    }

    @RequestMapping(value = "/deleteCollection",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData deleteCollection(CollectionRelic collectionRelic){
        adminService.deleteCollection(collectionRelic);
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    @RequestMapping(value = "/findCollectionByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findCollectionByName(String uName){
        List<AdminCollection> collectionList=adminService.findCollectionByName("%"+uName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("collectionList", collectionList);
        return responseData;
    }

    @RequestMapping(value = "/findGroupTypeCount",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findGroupTypeCount(){
        List<TypeCount> typeCountList=adminService.findGroupTypeCount();
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("typeCountList", typeCountList);
        return responseData;
    }
    @RequestMapping(value = "/findCollectionCount",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findCollectionCount(){
        List<TypeCount> typeCountList=adminService.findCollectionCount();
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("typeCountList", typeCountList);
        return responseData;
    }

    @RequestMapping(value = "/upload",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData upload(@RequestParam("file")MultipartFile file,@RequestParam("relic")String relicJson) throws IOException {
        ResponseData responseData = null;
        // 将 JSON 字符串转换为 Relic 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Relic relic = objectMapper.readValue(relicJson, Relic.class);
        DecadeType  decadeType=userService.findDecadeTypeByCode(relic.getDecadeType());
        GroupType  groupType=userService.findGroupTypeByCode(relic.getGroupType());
        LocalType localType=userService.findLocalTypeByCode(relic.getLocalType());
        relic.setDecadeType(decadeType.getLabel());
        relic.setGroupType(groupType.getLabel());
        relic.setLocalType(localType.getLabel());
        if (file.isEmpty()) {
            responseData = ResponseData.notFound();
            return responseData;
        }
        String fileUrl = ossService.uploadFile(file);
        relic.setImage(fileUrl);
        adminService.addRelic(relic);
        if (fileUrl != null) {
            responseData= ResponseData.ok(); // 返回文件的URL
            responseData.putDataValue("fileUrl", fileUrl);
        } else {
             responseData=ResponseData.customerError();
        }
        return responseData;
    }
    @RequestMapping(value = "/importRelicData")
    @ResponseBody
    public ResponseData importRelicData(@RequestParam("file") MultipartFile file) throws IOException {
        ResponseData responseData = null;
        if (file.isEmpty()) {
            System.out.println(file.getOriginalFilename());
            responseData = ResponseData.notFound();
            return responseData;
        }
        try {
            System.out.println(file.getInputStream());
            ImportResult result = relicImportService.processCSV(file.getInputStream());
            responseData= ResponseData.ok(); // 返回文件的URL
            responseData.putDataValue("result", result);

        } catch (IOException e) {
            responseData= ResponseData.ImportResultError(); // 返回文件的URL
            System.out.println(e.getMessage());
        }
        return responseData;
    }


}
